package vista;

import dao.DAOLocalidad;
import dao.DAOPropietario;
import dao.DAOProvincia;
import entidades.Localidad;
import entidades.Propietario;
import entidades.Provincia;
import java.awt.Color;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JOptionPane;

/**
 * Una instancia de esta clase contendrá las siguientes variables:
 * 
 *  modoVentana:  variable utilizada como parámetro para el modo en el
 *                              cual se utilizará la ventana, AGREGAR para creacion de
 *                              un nuevo propietario o MODIFICAR para modificar un 
 *                              propietario existente en el sistema.
 *  propietario:  instancia de una clase propietario, con la cual trabajará 
 *                              la ventana.
 *
 */

public class VAltaPropietario extends javax.swing.JDialog {

    private enum TipoAccion{
        AGREGAR,
        MODIFICAR;
    }
    TipoAccion modoVentana;
    Propietario propietario;
    
    /**
    * Creador de la ventana VAltaPropietario, la cual se utilizará tanto para 
    * la creacion de un propietario para agregar al sistema, o una modificación 
    * de un propietario seleccionado previamente.
    * 
    *   @param parent       elemento que llama a la funcion e instancia esta ventana
    * 
    *   @param propietario  instancia de propietario la cual será nula si la ventana 
    *                       se crea en modo AGREGAR, de lo contrario, la instancia 
    *                       propietario contendra la informacion con la que trabajará
    *                       la ventana
    *
    */
    
    public VAltaPropietario(java.awt.Frame parent, boolean modal, Propietario propietario, int id_propietario) {
        super(parent, modal);
        initComponents();
        
        //Si propietario es vacio, se debe crear un propietario nuevo
        if(propietario == null){
            this.propietario = new Propietario();
            if(id_propietario == -1){
                 System.out.println("Propietario vacio");
                 this.modoVentana = TipoAccion.AGREGAR;  
            }
            else{
                    System.out.println("Propietario lleno");
                    propietario = DAOPropietario.getByID(id_propietario);
                    this.modoVentana = TipoAccion.AGREGAR;
            }
        }
        //Si propietario no es vacio, es un propietario existente a modificar
        else{
            this.modoVentana = TipoAccion.MODIFICAR;
            this.propietario = propietario;
            
            //Precargamos los datos a modificar por el usuario
            this.tApellido.setText(propietario.getApellido());
            this.tNombre.setText(propietario.getNombre());
            //this.cbDocumento.setSelectedIndex(propietario.getTipoDoc());
            this.tDocumento.setText(propietario.getNumDoc());
            this.tCalle.setText(propietario.getCalle());
            this.tNumero.setText(Integer.toString(propietario.getNumero()));
            this.tPiso.setText(propietario.getPiso().toString());
            this.tDepartamento.setText(propietario.getDepartamento());
            this.tTelefono.setText(propietario.getTelefono());
            this.tEmail.setText(propietario.getEmail());
            this.tOculto.setText(Integer.toString(propietario.getIdPropietario()));
           
            //Recorro el combobox provincia para que coincida con el del propietario
            int cbTam = this.cbProvincia.getItemCount();
            int index = 0;
            for (; index < cbTam; index++)
                if (this.cbProvincia.getModel().getElementAt(index).equals(propietario.getLocalidad().getProvincia().getNombre())){
                    this.cbProvincia.setSelectedIndex(index);
                    break;
                }
            
            //Cargo las localidades de la provincia del propietario para realizar una busqueda luego
            Set localidades = VPrincipal.provincias.get(index).getLocalidads();
            Iterator i = localidades.iterator();

            cbLocalidad.removeAllItems();

            while(i.hasNext()) {
                this.cbLocalidad.addItem(((Localidad) i.next()).getNombre());
            }
            this.cbLocalidad.setEnabled(true);

            cbTam = this.cbLocalidad.getItemCount();
            for (index = 0; index < cbTam; index++)
                if (this.cbLocalidad.getModel().getElementAt(index).equals(propietario.getLocalidad().getNombre())){
                    this.cbLocalidad.setSelectedIndex(index);
                    break;
                }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        tApellido = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        cbDocumento = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        tDocumento = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tNombre = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        tCalle = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        tNumero = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        tPiso = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        tDepartamento = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        cbProvincia = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        cbLocalidad = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        tTelefono = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        tEmail = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        tOculto = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Alta Propietario");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Generales"));

        jLabel1.setText("Apellido:");

        tApellido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tApellidoActionPerformed(evt);
            }
        });
        tApellido.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tApellidoKeyTyped(evt);
            }
        });

        jLabel2.setText("Tipo Documento:");

        cbDocumento.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "DNI", "PP", "LC", "LE" }));
        cbDocumento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbDocumentoActionPerformed(evt);
            }
        });

        jLabel3.setText("Nro. Documento:");

        tDocumento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tDocumentoActionPerformed(evt);
            }
        });
        tDocumento.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tDocumentoKeyTyped(evt);
            }
        });

        jLabel4.setText("Nombres:");

        tNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tNombreActionPerformed(evt);
            }
        });
        tNombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tNombreKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tNombre))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tApellido)))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(tApellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cbDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tDocumento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Domicilio"));

        jLabel5.setText("Calle:");

        tCalle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tCalleActionPerformed(evt);
            }
        });
        tCalle.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tCalleKeyTyped(evt);
            }
        });

        jLabel6.setText("N°:");

        tNumero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tNumeroActionPerformed(evt);
            }
        });
        tNumero.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tNumeroKeyTyped(evt);
            }
        });

        jLabel7.setText("Piso:");

        tPiso.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tPisoKeyTyped(evt);
            }
        });

        jLabel8.setText("Depto:");

        tDepartamento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tDepartamentoActionPerformed(evt);
            }
        });
        tDepartamento.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tDepartamentoKeyTyped(evt);
            }
        });

        jLabel9.setText("Provincia:");

        cbProvincia.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "BUENOS AIRES", "CATAMARCA", "CHACO", "CHUBUT", "CORDOBA", "CORRIENTES", "ENTRE RIOS", "FORMOSA", "JUJUY", "LA PAMPA", "LA RIOJA", "MENDOZA", "MISIONES", "NEUQUEN", "RIO NEGRO", "SALTA", "SAN JUAN", "SAN LUIS", "SANTA CRUZ", "SANTA FE", "SANTIAGO DEL ESTERO", "TIERRA DEL FUEGO", "TUCUMAN" }));
        cbProvincia.setSelectedIndex(-1);
        cbProvincia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbProvinciaActionPerformed(evt);
            }
        });

        jLabel10.setText("Localidad:");

        cbLocalidad.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccone una Provincia" }));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tCalle, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbProvincia, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel10)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(tNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tPiso, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tDepartamento, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(cbLocalidad, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(tCalle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(tNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(tPiso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(tDepartamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(cbProvincia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(cbLocalidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Contacto"));

        jLabel11.setText("Teléfono:");

        tTelefono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tTelefonoKeyTyped(evt);
            }
        });

        jLabel12.setText("E-mail:");

        tEmail.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tEmailKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tTelefono)
                .addGap(18, 18, 18)
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(tTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12)
                    .addComponent(tEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/disk.png"))); // NOI18N
        jButton1.setText("Guardar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bGuardar(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/close_16.png"))); // NOI18N
        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelar(evt);
            }
        });

        tOculto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tOcultoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(tOculto, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(tOculto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        tOculto.hide();
        tOculto.disable();

        setSize(new java.awt.Dimension(425, 273));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void tApellidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tApellidoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tApellidoActionPerformed

    private void tNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tNombreActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tNombreActionPerformed

    private void tCalleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tCalleActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tCalleActionPerformed

    private void tNumeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tNumeroActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tNumeroActionPerformed

    private void tDepartamentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tDepartamentoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tDepartamentoActionPerformed

private void bCancelar(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelar
    this.dispose();
}//GEN-LAST:event_bCancelar

    private void cbDocumentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbDocumentoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbDocumentoActionPerformed

    private void tDocumentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tDocumentoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tDocumentoActionPerformed

private void cbProvinciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbProvinciaActionPerformed
        int provincia_seleccionada = cbProvincia.getSelectedIndex();
        
        Set localidades = VPrincipal.provincias.get(provincia_seleccionada).getLocalidads();
        Iterator i = localidades.iterator();
        
        cbLocalidad.removeAllItems();
        
        while(i.hasNext()) {
            this.cbLocalidad.addItem(((Localidad) i.next()).getNombre());
        }

        this.cbLocalidad.setSelectedIndex(-1);
        this.cbLocalidad.setEnabled(true);

}//GEN-LAST:event_cbProvinciaActionPerformed

private void bGuardar(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bGuardar

    //Inicializacion de las variables
    String apellido = tApellido.getText();
    String nombre = tNombre.getText();
    int tipo_documento = cbDocumento.getSelectedIndex();
    String numero_documento = tDocumento.getText(); 
    String calle = tCalle.getText();
    Integer numero_calle = new Integer(0);  //
    Integer piso = new Integer(0);       //
    String departamento = tDepartamento.getText();
    String telefono = tTelefono.getText();
    String email = tEmail.getText();
    
    //Chequeo de las variables de entrada
    if(apellido.isEmpty()){
           JOptionPane.showMessageDialog(this, "Debe completar el Apellido", "Error! Apellido", JOptionPane.ERROR_MESSAGE);
           return;
    }
    if(nombre.isEmpty()){
           JOptionPane.showMessageDialog(this, "Debe completar el Nombre", "Error! Nombre", JOptionPane.ERROR_MESSAGE);
           return;
    }
    if(numero_documento.isEmpty()){
           JOptionPane.showMessageDialog(this, "Debe completar el Número de Documento", "Error! Numero de Documento", JOptionPane.ERROR_MESSAGE);
           return;
    }
    if(calle.isEmpty()){
           JOptionPane.showMessageDialog(this, "Debe completar el nombre de la Calle", "Error! Nombre de la Calle", JOptionPane.ERROR_MESSAGE);
           return;
    }
     if(!tNumero.getText().isEmpty()) {
            try {
                numero_calle = Integer.parseInt(tNumero.getText());
                tNumero.setForeground(Color.black);
            }
            catch(Exception e) {
                JOptionPane.showMessageDialog(this, "El numero de calle debe ser un número entero", "Error! Numero de calle", JOptionPane.ERROR_MESSAGE);
                tNumero.setForeground(Color.red);
                return;
            }
      }
      else{
           JOptionPane.showMessageDialog(this, "Debe completar el Número de Calle", "Error! Número de Calle", JOptionPane.ERROR_MESSAGE);
           return;
      }
            
       if(!tPiso.getText().isEmpty()) {
                piso = Integer.parseInt(tPiso.getText());
                tPiso.setForeground(Color.black);
       }
       
       if(cbProvincia.getSelectedIndex() < 0){
           JOptionPane.showMessageDialog(this, "Debe seleccionar una Provincia", "Error! Provincia", JOptionPane.ERROR_MESSAGE);
           return;
       }
       
       if(cbLocalidad.getSelectedIndex() < 1){
           JOptionPane.showMessageDialog(this, "Debe seleccionar una Localidad", "Error! Localidad", JOptionPane.ERROR_MESSAGE);
           return;
       }

       //Provincia y localidad del propietario
       Provincia provincia = DAOProvincia.getByNombre(cbProvincia.getSelectedItem().toString());
       Localidad localidad;
       localidad = DAOLocalidad.getByProvinciaLocalidad(provincia.getIdProvincia(), cbLocalidad.getSelectedItem().toString());
       
      
       //Seteo de variables
       propietario.setApellido(apellido);
       propietario.setNombre(nombre);
       propietario.setTipoDoc(DAOPropietario.setTipoDocEnum(tipo_documento));
       propietario.setNumDoc(numero_documento);
       propietario.setCalle(calle);
       propietario.setNumero(numero_calle);
       propietario.setPiso(piso);
       propietario.setDepartamento(departamento);
       propietario.setTelefono(telefono);
       propietario.setEmail(email);
       propietario.setLocalidad(localidad);
       
       //Diferencia entre las acciones de AGREGAR y MODIFICAR
       if(this.modoVentana == TipoAccion.AGREGAR){
           DAOPropietario.guardarOActualizarObjeto(propietario);
           System.out.println("Datos Agregado");
           this.dispose();
       }
       if(this.modoVentana == TipoAccion.MODIFICAR){
 
           DAOPropietario.guardarOActualizarObjeto(propietario);
           System.out.println("Datos Modificado");
           this.dispose();
       }
}//GEN-LAST:event_bGuardar

    private void tOcultoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tOcultoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tOcultoActionPerformed

    private void tApellidoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tApellidoKeyTyped
        if (this.tApellido.getText().length() == 30)
            evt.consume();
    }//GEN-LAST:event_tApellidoKeyTyped

    private void tNombreKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tNombreKeyTyped
        if (this.tNombre.getText().length() == 30)
            evt.consume();
    }//GEN-LAST:event_tNombreKeyTyped

    private void tDocumentoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tDocumentoKeyTyped
        if (this.tDocumento.getText().length() == 8)
            evt.consume();
    }//GEN-LAST:event_tDocumentoKeyTyped

    private void tCalleKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tCalleKeyTyped
        if (this.tCalle.getText().length() == 40)
            evt.consume();
    }//GEN-LAST:event_tCalleKeyTyped

    private void tNumeroKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tNumeroKeyTyped
        if (this.tNumero.getText().length() == 5)
            evt.consume(); 
    }//GEN-LAST:event_tNumeroKeyTyped

    private void tPisoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tPisoKeyTyped
        if (this.tPiso.getText().length() == 2)
            evt.consume();
    }//GEN-LAST:event_tPisoKeyTyped

    private void tDepartamentoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tDepartamentoKeyTyped
        if (this.tDepartamento.getText().length() == 3)
            evt.consume();
    }//GEN-LAST:event_tDepartamentoKeyTyped

    private void tEmailKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tEmailKeyTyped
        if (this.tEmail.getText().length() == 40)
            evt.consume();
    }//GEN-LAST:event_tEmailKeyTyped

    private void tTelefonoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tTelefonoKeyTyped
        if (this.tTelefono.getText().length() == 12)
            evt.consume();
    }//GEN-LAST:event_tTelefonoKeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VAltaPropietario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VAltaPropietario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VAltaPropietario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VAltaPropietario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                VAltaPropietario dialog = new VAltaPropietario(new javax.swing.JFrame(), true,null,-1);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbDocumento;
    private javax.swing.JComboBox cbLocalidad;
    private javax.swing.JComboBox cbProvincia;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField tApellido;
    private javax.swing.JTextField tCalle;
    private javax.swing.JTextField tDepartamento;
    private javax.swing.JTextField tDocumento;
    private javax.swing.JTextField tEmail;
    private javax.swing.JTextField tNombre;
    private javax.swing.JTextField tNumero;
    private javax.swing.JTextField tOculto;
    private javax.swing.JTextField tPiso;
    private javax.swing.JTextField tTelefono;
    // End of variables declaration//GEN-END:variables
}
